#include "UdpClient.hpp"
#include "Common.hpp"
#include <iostream>
#include <cstring>
#include <string>
#include <cstdlib>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <pthread.h>
#include <signal.h>

int sockfd = -1;
struct sockaddr_in server;

//如果有用户退出, 那么我会知道是哪个用户退出
void ClientQuit(int signo)
{
    (void)signo;
    const std::string quit = "QUIT";
    int n = ::sendto(sockfd, quit.c_str(),quit.size(), 0, CONV(&server),sizeof(server));
    exit(0);
}

void *Recver(void *args)
{
    while(true)
    {
        (void)args;
        struct sockaddr_in temp;
        socklen_t len = sizeof(temp);
        char buffer[1024];
        int n = ::recvfrom(sockfd, buffer, sizeof(buffer) - 1, 0, CONV(&temp), &len);
        if(n > 0)
        {
            buffer[n] = 0;
            std::cerr << buffer << std::endl;
        }
    }
}

int main(int argc, char* argv[])
{
    if(argc != 3)
    {
        std::cerr << "Usage" << argv[0] << "serverip serverport" << std::endl;
        Die(USAGE_ERR);
    }
    signal(2, ClientQuit);

    std::string serverip = argv[1];
    uint16_t serverport = std::stoi(argv[2]);

    //1.创建socket
    sockfd = ::socket(AF_INET, SOCK_DGRAM, 0);
    if(sockfd < 0)
    {
        std::cerr << "socket error" << std::endl;
        Die(SOCKET_ERR);
    }
    //1.填充server信息
    memset(&server, 0, sizeof(server));
    server.sin_family = AF_INET;
    server.sin_port = ::htons(serverport);
    server.sin_addr.s_addr = ::inet_addr(serverip.c_str());

    //主线程用来发送信息, 在创建一个线程用来接收消息
    pthread_t tid;
    pthread_create(&tid, nullptr, Recver, nullptr);

    //1.2 用户端启动时, 给服务器推送消息即可
    const std::string online = "...来了哈!";
    int n = ::sendto(sockfd, online.c_str(), online.size(), 0, CONV(&server),sizeof(server));

    //2.启动用户端
    while(true)
    {
        std::cout << "Please Enter# ";
        std::string message; 
        std::getline(std::cin, message);
        int n = ::sendto(sockfd, message.c_str(),message.size(), 0, CONV(&server), sizeof(server));
        (void)n;
    }
    return 0;
}   